

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

public class client extends JFrame implements Runnable{
	private JPanel panel;
	//loginButton为登录按钮
	private JButton loginButton;
	//quitButton为退出按钮
	private JButton quitButton;
	//LoginSuccessLabel为登录成功标签
	private JLabel LoginSuccessLabel;
	//LocalUsersLabel为本地用户名标签
	private JLabel LocalUsersLabel;
	//UserObjectLabel为对象用户名标签
	private JLabel UserObjectLabel;
	//LocalUsersField为本地用户名的文本框
	private JTextField LocalUsersField;
	//UserObjectField为对象用户名的文本框
	private JTextField UserObjectField;
	//ChatContentLbel为聊天内容的标签
	private JLabel ChatContentLbel;
	//OnlineUserslabel为在线用户的标签
	private JLabel OnlineUserslabel;
	//SendMessageLabel为发送信息的标签
	private JLabel SendMessageLabel;
	//ChatContentArea为聊天内容的文本域
	private JTextArea ChatContentArea;
	//OnlineUsersArea为在线用户的文本域
	private JTextArea OnlineUsersArea;
	//SendMessageArea为发送信息的文本域
	private  JTextArea SendMessageArea;
	//ChatContentScroll为装载ChatContentArea的滚动面板
	private JScrollPane ChatContentScroll;
	//SendMessageScroll为装在SendMessageArea的滚动面板
	private JScrollPane SendMessageScroll;
	//SendButton为发送按钮
	private JButton SendButton;
	private BufferedReader in;
	private PrintWriter out;
	private Socket socket;
	private final String spliter = "--,--";
	public client(){
		this.setTitle("客户端界面2");
		this.setBounds(650, 100, 650, 750);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		panel = new JPanel();
		panel.setLayout(null);
		this.setContentPane(panel);
		loginButton = new JButton("登录");
		Font font = new Font("宋体",Font.BOLD,10);
		loginButton.setFont(font);
		loginButton.setBounds(225,30,60,25);
		loginButton.setBackground(Color.lightGray);
		LoginSuccessLabel = new JLabel("登录成功!");
		LoginSuccessLabel.setBounds(290, 5, 70, 80);
		//jtextField1为本地用户名
		LocalUsersField = new JTextField(10);
		LocalUsersField.setBounds(120, 30, 80, 25);
		//jextField2为对象用户名
		UserObjectField = new JTextField(10);
		UserObjectField.setBounds(120, 60, 80, 25);
		LocalUsersLabel = new JLabel("本地用户名:");
		LocalUsersLabel.setBounds(30, 30, 80, 25);
		quitButton = new JButton("退出");
		quitButton.setBounds(225, 60, 60, 25);
		quitButton.setBackground(Color.lightGray);
		quitButton.setFont(font);
		UserObjectLabel = new JLabel("对象用户名:");
		UserObjectLabel.setBounds(30, 60, 80, 25);
		ChatContentLbel = new JLabel("聊天内容:");
		ChatContentLbel.setBounds(30, 100, 80, 25);
		OnlineUserslabel = new JLabel("在线用户:");
		OnlineUserslabel.setBounds(465, 30, 80, 25);
		SendMessageLabel = new JLabel("发送信息:");
		SendMessageLabel.setBounds(30, 430, 80, 25);
		ChatContentArea = new JTextArea(300,300);
		ChatContentArea.setLineWrap(true);
		OnlineUsersArea = new JTextArea(150,200);
		OnlineUsersArea.setLineWrap(true);
		OnlineUsersArea.setBounds(465, 65, 150, 200);
		SendMessageArea = new JTextArea(300,250);
		//jextArea1为聊天内容的文本框
		ChatContentScroll = new JScrollPane(ChatContentArea);
		ChatContentScroll.setBounds(120, 120, 300, 300);
		//jextArea3为发送信息的文本框
		SendMessageScroll = new JScrollPane(SendMessageArea);
		SendMessageScroll.setBounds(120, 430, 300, 250);
		SendButton = new JButton("发送");
		SendButton.setBounds(465, 655, 60, 25);
		SendButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				// TODO 自动生成的方法存根
				out.write(UserObjectField.getText() + ":" + SendMessageArea.getText());
				out.flush();
			}
			
		});
		panel.add(SendButton);
		panel.add(SendMessageScroll);
		panel.add(SendMessageLabel);
		panel.add(OnlineUsersArea);
		panel.add(ChatContentScroll);
		panel.add(OnlineUserslabel);
		panel.add(ChatContentLbel);
		panel.add(UserObjectField);
		panel.add(UserObjectLabel);
		panel.add(quitButton);
		panel.add(LocalUsersLabel);
		panel.add(LocalUsersField);
		panel.add(loginButton);
		panel.add(LoginSuccessLabel);
		panel.updateUI();
		LoginSuccessLabel.hide();
		loginButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根

				//发送  fushiyong --,-- login
				String username = LocalUsersField.getText();
				String requestStr = getLoginRequestString(username);
				out.write(requestStr);
				out.flush();
				try {
					socket.shutdownOutput();
				} catch (IOException ex) {
					ex.printStackTrace();
				}


				BufferedReader br = null;
				try {
					br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				} catch (IOException ex) {
					ex.printStackTrace();
				}


				String allLine ="";
				try {
					String line = "";
					while((line = br.readLine()) != null){
						allLine+=line;
					}

					String resp = allLine;
					if(resp.contains("成功")){
						LoginSuccessLabel.setVisible(true);
					}else{
						JOptionPane.showConfirmDialog(client.this,resp);
					}
					return;
				} catch (IOException ex) {
					ex.printStackTrace();
				}


			}
			
		});
		try {
			socket = new Socket("127.0.0.1",4545);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
		} catch (UnknownHostException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		
		setVisible(true);
		
		
	}

	private String getLoginRequestString(String username) {
		String result = username + spliter + CommandConstant.LOGIN_COMMAND ;
		return result;
	}

	@Override
	public void run() {
		// TODO 自动生成的方法存根
    	String news;
		while(true) {
			try {
				ChatContentArea.append(UserObjectField.getText() + ":" + in.readLine() + System.lineSeparator());
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new Thread(new client()).start();
	}

}
